package com.neutech.maplestory.client.entity;

import com.neutech.maplestory.client.client.MapleStoryClint;
import com.neutech.maplestory.client.constant.Constant;
import com.neutech.maplestory.client.util.ImageUtil;

import java.awt.*;
import java.util.List;
import java.util.Random;
/**
 * CLASS:Arrow：
 * TODU:
 * date:2021/1/28、18:02
 * version:0.1
 */
public class Arrow extends AbstractMapleStoryObject{
    public static Image[] images = {
            ImageUtil.getImage("arrow_right"),
            ImageUtil.getImage("arrow_left"),
    };

    public Arrow(){
        this.speed= 10;
        this.live=true;
        this.width= images[0].getWidth(null);
        this.heigtht= images[0].getHeight(null);
    }

    public Arrow (MapleStoryClint msc,int x, int y, Direction dir){
        this();
        this.msc=msc;
        this.x = x;
        this.y = y;
        this.dir=dir;

        /*if(dir==Direction.RIGHT) {
            this.img = images[0];
        }else {
            this.img=images[1];
        }*/

    }

    @Override
    public void move() {
        switch (dir){
            case RIGHT:
                this.x+=speed;
                break;
            case LEFT:
                this.x-=speed;
                break;
            default:
                break;
        }
        outOfBound();
    }


    @Override
    public void draw(Graphics g) {
        if(!live){
            msc.arrows.remove(this);
            return;
        }
        switch (dir){
            case RIGHT:
                g.drawImage(images[0],x,y,null);
                break;
            case LEFT:
                g.drawImage(images[1],x,y,null);
                break;
            default:
                break;
        }
        move();
    }
    /**
     *出界判断
     */
    private void outOfBound(){
        if(this.x<=-500||this.x>= Constant.GAME_WIDTH+500){
            this.live=false;
        }
    }

    public Random r = new Random();
    private boolean hit(Mob mob){
        if(this.live && mob.action!=Action.DIE&&this.getRectangle().intersects(mob.getRectangle())){
            this.live=false;
            //msc.hero.hitValue=r.nextInt(msc.hero.ATT-5)+8;//8~12
            msc.hero.hitValue= (int) (msc.hero.ATT*(1+r.nextDouble()));
            //创建出伤害值
            Power power = new Power(msc,mob.x,mob.y);
            mob.HP-=power.value;
            msc.powers.add(power);
            if(mob.HP<=0){
                mob.action=Action.DIE;
                msc.hero.EX+=50;
                if(r.nextInt(100)<100){
                    Item item = new Item(msc,mob.x+mob.width/2,mob.y+mob.heigtht-30,r.nextInt(4));
                    msc.items.add(item);
                }
            }

           // mob.live=false;
            return true;
        }
        return false;
    }

    /**
     * 一只和
     * @param mobs
     * @return
     */
    public boolean hit(List<Mob> mobs){
        for (int i = 0; i < mobs.size(); i++) {
            Mob mob = mobs.get(i);
            if(hit(mob)){
                return true;
            }
        }
        return false;
    }
}
